package com.ape.mapsa.report;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.j2ee.servlets.BaseHttpServlet;

public class AbstractReportBean {
	public enum ExportOption {
		PDF, HTML, EXCEL, RTF
	}

	private ExportOption exportOption;
	private final String COMPILE_DIR = "/report/design/";
	// private String compileFileName = "productlist";//name of your compiled
	// report file
	private String message;

	public AbstractReportBean() {
		super();
		setExportOption(ExportOption.PDF);
	}

	protected void prepareReport() throws JRException, IOException {
		ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();

		ServletContext context = (ServletContext) externalContext.getContext();
		HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
		HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();

		// /////////////////
		// Connection conn = null;
		// try {
		// conn = Database.getConnection();
		// } catch (Exception ex) {
		// ex.printStackTrace();
		// }

		// ///////////////////

		// JasperPrint jasperPrint = ReportConfigUtil.fillReport(reportFile,
		// getReportParameters(), conn);

		JasperPrint jasperPrint = JasperFillManager.fillReport("com/ape/mapsa/report/report1.jasper", null);

		if (getExportOption().equals(ExportOption.HTML)) {
			ReportConfigUtil.exportReportAsHtml(jasperPrint, response.getWriter());
		} else if (getExportOption().equals(ExportOption.EXCEL)) {
			ReportConfigUtil.exportReportAsExcel(jasperPrint, response.getWriter());
		} else {
			request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
			response.sendRedirect(request.getContextPath() + "/report/" + getExportOption());
		}

		FacesContext.getCurrentInstance().responseComplete();
	}

	public ExportOption getExportOption() {
		return exportOption;
	}

	public void setExportOption(ExportOption exportOption) {
		this.exportOption = exportOption;
	}

	protected Map<String, Object> getReportParameters() {
		return new HashMap<String, Object>();
	}

	protected String getCompileDir() {
		return COMPILE_DIR;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
}
